package com.xzq.daoimpl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xzq.dao.AddressDao;
import com.xzq.entity.Address;
import com.xzq.entity.Page;
import com.xzq.entity.User;
import com.xzq.entity.page.PageSize;
import com.xzq.util.HibernatePage;
@Repository
public class AddressDaoImpl extends HibernatePage<Address> implements AddressDao {

	@Override
	public List<Address> findOneUserAllAddress(User user) {
		String hql = "from Address a where a.user.id = ?";
		Object[] obj = {user.getId()};
		List<Address> list = (List<Address>) template.find(hql, obj);
		return list;
	}

	@Override
	public Page findAllAddress(Page page, User user) {
		String hql = "from Address a where a.user.id = ?";
		Object[] obj = {user.getId()};
		page = findHibernatePage(page, hql, obj, PageSize.addressSize);
		return page;
	}

	@Override
	public Page findAllAddressByAdmin(Page page) {
		String hql = "from Address";
		page = findHibernatePage(page, hql, null, PageSize.addressSize);
		return page;
	}

	@Override
	public int addAddress(Address address) {
		Serializable s = template.save(address);
		int count = (Integer) s;
		return count;
	}

	@Override
	public int modifyAddress(Address address) {
		try {
			template.update(address);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteAddress(Address address) {
		try {
			template.delete(address);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Address findOneAddress(Address address) {
		address = template.get(Address.class, address.getId());
		return address;
	}

}
